package wang.hu.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.base.Area;
import wang.hu.bos.service.AreaService;
import wang.hu.bos.utils.PinYin4jUtils;
import wang.hu.bos.utils.PinYinDuoYinUtils;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {

	@Autowired
	private AreaService areaService;
	private File file;
	private String fileFileName;

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Action(value = "Area_save", results = { @Result(name = "success", location = "./pages/base/area.html", type = "redirect") })
	public String save() {
		areaService.save(model);
		return SUCCESS;
	}

	@Action(value = "area_batchImport")
	public String batchImport() {
		List<Area> list = new ArrayList<Area>();
		Sheet sheet = null;
		try {
			if (fileFileName.endsWith("xls")) {
				HSSFWorkbook hssfWorkbook = new HSSFWorkbook(
						new FileInputStream(file));
				sheet = hssfWorkbook.getSheetAt(0);
			} else if (fileFileName.endsWith("xlsx")) {
				XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
						file));
				sheet = workbook.getSheetAt(0);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null
					|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			// 生成编码和简码
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);
			StringBuilder builder = new StringBuilder();
			String citycode = PinYinDuoYinUtils.convertChineseToPinyin(city,
					true);
			String[] shortcodeArray = PinYin4jUtils.getHeadByString(province
					+ city + district);
			area.setCitycode(citycode);
			for (String string : shortcodeArray) {
				builder.append(string);
			}
			area.setShortcode(builder.toString());
			list.add(area);
		}
		areaService.batchSave(list);
		return NONE;
	}

	// 分页查询
	@Action(value = "area_pageData", results = { @Result(name = "success", type = "json") })
	public String pageData() {
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Area> specification = new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p2 = cb.like(root.get("province")
							.as(String.class), "%" + model.getProvince() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p3 = cb.like(root.get("city").as(String.class),
							"%" + model.getCity() + "%");
					list.add(p3);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p4 = cb.like(root.get("district")
							.as(String.class), "%" + model.getDistrict() + "%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};

		Page<Area> pagedata = areaService.pageData(specification, pageable);
		this.pushPageableToValueStack(pagedata);
		return SUCCESS;
	}
}
